package com.right.health.app.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.DrugReminderInfo;
import com.right.health.app.bean.DrugReminderResponse;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.RemindApi;
import com.right.health.app.ui.me.adapter.DrugReminderAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_CHOOSE_FAMILY;

/*
 * Created by hopeliao on 2019-09-29
 * 用药提醒
 *
 */
public class DrugRemindActivity extends BaseActivity implements View.OnClickListener, OnLastItemVisibleListener,
        SwipeRefreshLayout.OnRefreshListener, OnHttpListener {

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;
    private static final int HTTP_CODE_MODIFY_0 = 3;
    private static final int HTTP_CODE_MODIFY_1 = 4;

    private static final int REQUEST_CODE_FAMILY = 1;

    private DrugReminderAdapter mAdapter;

    private SwipeListRefreshLayout mRefreshView;

    private int mPageNo = 1;

    private RemindApi mRemindApi;

    private TextView mUserTv;

    public List<DrugReminderInfo> mDataSource = new ArrayList<>();

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.drug_reminder);
    }

    @Override
    public void findView() {
        mUserTv = (TextView) findViewById(R.id.tv_user);

        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        findViewById(R.id.ib_add).setOnClickListener(this);

        ListView listView = (ListView) findViewById(R.id.listview);
        mAdapter = new DrugReminderAdapter(this);
        mAdapter.setOnClickListener(this);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DrugReminderInfo info = (DrugReminderInfo) parent.getItemAtPosition(position);
                if (info != null) {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(BroadCastKey.DATA, info);
                    startIntent(DrugRemindDetailActivity.class, bundle);
                }

            }
        });

        findViewById(R.id.tv_choose_family).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mRemindApi = new RemindApi();
        mRemindApi.setListener(this);

        mUserTv.setText(App.getInstance().getCurrentMember().name);

        onReceiveBroadcast(BroadCastCode.RREFRESH_DRUGREINDE, null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_add:
                startIntent(DrugReminderAddActivity.class);
                break;
            case R.id.tv_choose_family:
                FamilyActivity.makeActivity(this, true, REQUEST_CODE_FAMILY);
                break;
            case R.id.rl_not_take_medicine: //未服用点击
                handleState((int) v.getTag(), 1);
                break;
            case R.id.rl_take_medicine: //已服用点击
                handleState((int) v.getTag(), 0);
                break;
        }
    }

    private void handleState(int pos, int state) {
        DrugReminderInfo info = mDataSource.get(pos);

        mRemindApi.setTag(pos);
        if(state == 0) {
            mRemindApi.addRequestCode(HTTP_CODE_MODIFY_0);
        } else {
            mRemindApi.addRequestCode(HTTP_CODE_MODIFY_1);
        }

        mRemindApi.modifyTakeMedicine(info.cycletimesid, state);
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if (intent == BroadCastCode.RREFRESH_DRUGREINDE) {
            mRefreshView.setRefreshing(true);
            onRefresh();
        }else if(intent == REFRESH_CHOOSE_FAMILY) {
            mUserTv.setText(App.getInstance().getCurrentMember().name);
        }
    }

    @Override
    public void onLastItemVisible() {
        mPageNo++;
        mRemindApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mRemindApi.getDrugRemindList(mPageNo, App.getInstance().getCurrentMember().id);
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;
        mRemindApi.addRequestCode(HTTP_CODE_REFRESH);
        mRemindApi.getDrugRemindList(mPageNo, App.getInstance().getCurrentMember().id);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_FAMILY) {
                Bundle bundle = data.getExtras();
                if (bundle != null) {
                    FamilyResponse.FamilyInfo mFamilyInfo = bundle.getParcelable(BroadCastKey.DATA);
                    if (mFamilyInfo != null) {
                        mUserTv.setText(mFamilyInfo.membername);
                        onReceiveBroadcast(BroadCastCode.RREFRESH_DRUGREINDE, null);
                    }
                }
            }
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_MODIFY_0 || request.requestCode == HTTP_CODE_MODIFY_1) {
                int pos = (int)request.tag;
                String target = request.requestCode == HTTP_CODE_MODIFY_0? "0": "1";

                DrugReminderInfo info = mDataSource.get(pos);
                info.iseat = target;
                mAdapter.setDataSource(mDataSource);
            } else if (response.targetData instanceof DrugReminderResponse) {
                DrugReminderResponse drugReminderResponse = response.cast(DrugReminderResponse.class);
                if (request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }

                if (drugReminderResponse.page != null) {
                    mRefreshView.setCanLoadMore(!drugReminderResponse.page.lastPage);
                }

                mDataSource.addAll(drugReminderResponse.reminds);
                mAdapter.setDataSource(mDataSource);
            }
        }
    }
}
